package com.dreamsail.core.frame;

import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


@Configuration
public class MvcConfig implements WebMvcConfigurer {

    private final List<ServiceConfig> serviceConfigList;

    public MvcConfig(List<ServiceConfig> serviceConfigList) {
        this.serviceConfigList = serviceConfigList;
    }

    /**
     * 添加允许跨域
     *
     * @param registry
     */
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedOrigins("*")
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"});
    }

    /**
     * 请求头处理
     *
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // TODO
//        for (ServiceConfig serviceConfig : serviceConfigList) {
//            configurer.addPathPrefix(serviceConfig.getRequestPrefix(), c ->
//                    c.getPackage().getName().equals(serviceConfig.getPackageStr())
//            );
//        }
    }


    public void addInterceptors(InterceptorRegistry registry){
        //  添加登录拦截
        registry.addInterceptor(new LoginRegistry()).addPathPatterns("/**");
    }
}
